
//
//  ZFHomeViewController.swift
//  WeiBoAPP
//
//  Created by 张学飞 on 2018/12/28.
//  Copyright © 2018 zxf. All rights reserved.
//

import UIKit

class ZFHomeViewController: ZFBaseViewController {
    
    //    private lazy var statusList = [String]()
    //    private lazy var listViewModel = HomeViewModel()
    private lazy var listViewModel = HomeViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        
        //        let cell3 = HomeFloorCell3.init()
        
        test_data()
        
    }
    
    func test_data() {
        let keys = "user.key"
        var value = UserDefaults.standard.object(forKey: keys)
        print("查询：\(value ?? "")")
        UserDefaults.standard.setValue("hhhhhhhh", forKey: keys)
        print("设置一下")
        value = UserDefaults.standard.object(forKey: keys)
        print("查询：\(value ?? "")")
        value = UserDefaults.standard.removeObject(forKey:keys)
        print("删除一下")
        
        value = UserDefaults.standard.removeObject(forKey:keys)
        print("最后的值查询：\(value ?? "")")
        
    }
    
    // MARK:
    override func loadData() {
        
        listViewModel.loadDataHandle { isSuccess in
            self.refreshController?.endRefreshing()
            self.isPullup = false
            self.tableView?.reloadData()
        }
    }
    
    // 刷新
    override func refreshData() {
        
        self.loadData()
    }
}

// MARK: UI
extension ZFHomeViewController{
    
    override func setupTableView() {
        super.setupTableView()
        
        tableView?.register(UINib.init(nibName: "HomeFloorCell", bundle: nil), forCellReuseIdentifier: "floor1")
        tableView?.register(UINib.init(nibName: "HomeFloorCell2", bundle: nil), forCellReuseIdentifier: "floor2")
        tableView?.register(UINib.init(nibName: "HomeFloorCell3", bundle: nil), forCellReuseIdentifier: "floor3")
        tableView?.register(UINib.init(nibName: "HomeFloorCell4", bundle: nil), forCellReuseIdentifier: "floor4")
        tableView?.register(UINib.init(nibName: "HomeFloorCell5", bundle: nil), forCellReuseIdentifier: "floor5")
        tableView?.separatorStyle = .none;
        tableView?.rowHeight = UITableViewAutomaticDimension;
        tableView?.estimatedRowHeight = 60;
        
    }
}

// MARK: Delegate
extension ZFHomeViewController {
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.listViewModel.dataList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell:UITableViewCell
        
        if indexPath.row == 0 {
            cell = tableView.dequeueReusableCell(withIdentifier: "floor1")!
        }
        else if indexPath.row == 1 {
            cell = tableView.dequeueReusableCell(withIdentifier: "floor2")!
        }
        else if indexPath.row == 2 {
            cell = tableView.dequeueReusableCell(withIdentifier: "floor3")!
            let  floor3 = cell as! HomeFloorCell3;
            floor3.model = self.listViewModel.dataList[indexPath.row]
            floor3.mDelegate = self
            
            floor3.sblock = {(str:String) in
                print("sblock点击事件 \(str) \(indexPath)")
            }
            
            floor3.cellDidSelectBlock = {(selModel:HomeModel)in
                print("cellDidSelectBlock\(selModel.url ?? "")")
            }
            
        }
        else if indexPath.row == 3 {
            cell = tableView.dequeueReusableCell(withIdentifier: "floor4")!
        }
        else if indexPath.row == 4 {
            cell = tableView.dequeueReusableCell(withIdentifier: "floor5")!
        }
        
        else{
            cell = tableView.dequeueReusableCell(withIdentifier: "floor1")!
            
        }
        return cell
    }
    
}



// 05.1 遵守代理协议
/** extension
 -- extension 类似于 OC 中的 Category
 -- 格式: extension 类名 {}
 -- Swift中 代码可读性差, 通过 extension 完成代码分块, 增强可读性, 单一模块单独处理, 增大了当前类的作用域
 -- extension 中 可以添加计算型属性 不能添加存储型属性; 可以定义便利构造函数 不能定义指定构造函数
 */

extension ZFHomeViewController :HomeFloorCell3Delegate {
    
    // 实现代理方法
    func HomeFloorDidSelectAction(model: HomeModel) {
        
        print("\(model.title ?? "") \n换行一下：url: \(model.url ?? "")")
     
        
//        let nextpage = ZFHomeNextViewController.
        
        
    }
    
}
